package com.iqiyi.pps.epg.core.service.vod;

import com.iqiyi.pps.epg.api.model.web.query.TFilter;
import com.iqiyi.pps.epg.api.model.web.query.TPage;
import com.iqiyi.pps.epg.api.model.web.vod.*;
import com.iqiyi.pps.epg.core.dao.base.Page;
import com.iqiyi.pps.epg.core.dao.base.PropertyFilter;
import com.iqiyi.pps.epg.core.dao.vod.VodEpisodeLogDao;
import com.iqiyi.pps.epg.core.dao.vod.VodLogDao;
import com.iqiyi.pps.epg.core.dao.vod.VodRecordDao;
import com.iqiyi.pps.epg.core.model.vod.VodEpisodeLog;
import com.iqiyi.pps.epg.core.model.vod.VodLog;
import com.iqiyi.pps.epg.core.model.vod.VodRecord;
import com.iqiyi.pps.epg.core.utils.DateTimeUtil;
import com.iqiyi.pps.epg.core.utils.TransUtil;
import org.hibernate.Query;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by karkarrotxu on 2017/5/8.
 */
@Service
@Transactional
public class VodServiceImpl implements IVodService {
    @Autowired
    private VodLogDao vodLogDao;

    @Autowired
    private VodEpisodeLogDao vodEpisodeLogDao;

    @Autowired
    private VodRecordDao vodRecordDao;

    @Autowired
    private IVodAsyncService vodAsyncService;


    @Override
    public long saveLog(VodLog log) {
        vodLogDao.saveOnly(log);
        return log.getId();
    }

    @Override
    @Transactional(propagation = Propagation.NOT_SUPPORTED, readOnly = true)
    public TVodLogPage findLogPage(TPage page, TFilter filter) {
        List<PropertyFilter> filterList = TransUtil.builderFilterList(filter);
        Page<VodLog> dbPage = TransUtil.buildPage(page);
        Page<VodLog> pageData = vodLogDao.findPage(dbPage, filterList);
        List<VodLog> dataList = pageData.getResult();
        TVodLogPage vodLogPage = new TVodLogPage();
        page.setTotalCount(pageData.getTotalCount());
        vodLogPage.setTPage(page);
        List<TVodLog> list =vodLogDao.transToTList(dataList);
        vodLogPage.setResult(list);
        return vodLogPage;
    }

    @Override
    public long saveEpisodeLog(VodEpisodeLog log) {
        vodEpisodeLogDao.saveOnly(log);
        return log.getId();
    }

    @Override
    public TVodEpisodeLogPage findEpisodeLogPage(TPage page, TFilter filter) {
        List<PropertyFilter> filterList = TransUtil.builderFilterList(filter);
        Page<VodEpisodeLog> dbPage = TransUtil.buildPage(page);
        Page<VodEpisodeLog> pageData = vodEpisodeLogDao.findPage(dbPage, filterList);
        List<VodEpisodeLog> dataList = pageData.getResult();
        TVodEpisodeLogPage vodLogPage = new TVodEpisodeLogPage();
        page.setTotalCount(pageData.getTotalCount());
        vodLogPage.setTPage(page);
        List<TVodEpisodeLog> list =vodEpisodeLogDao.transToTList(dataList);
        vodLogPage.setResult(list);
        return vodLogPage;
    }

    @Override
    @Transactional(propagation = Propagation.NOT_SUPPORTED, readOnly = true)
    public TVodRecordPage findRecordPage(TPage page, TFilter filter) {
        List<PropertyFilter> filterList = TransUtil.builderFilterList(filter);
        Page<VodRecord> dbPage = TransUtil.buildPage(page);
        Page<VodRecord> pageData = vodRecordDao.findPage(dbPage, filterList);
        List<VodRecord> dataList = pageData.getResult();
        TVodRecordPage vodLogPage = new TVodRecordPage();
        page.setTotalCount(pageData.getTotalCount());
        vodLogPage.setTPage(page);
        List<TVodRecord> list =vodRecordDao.transToTList(dataList);
        vodLogPage.setResult(list);
        return vodLogPage;
    }




    @Override
    public long addRecord(VodRecord entity) {
        vodRecordDao.saveOnly(entity);
        if(  entity.getId() > 0){
            vodAsyncService.addRecordTask( entity );
        }
        return entity.getId();
    }



    @Override
    public void editRecord(VodRecord entity,boolean timeChange) {
        vodRecordDao.save(entity);
        if(timeChange){
            vodAsyncService.addRecordTask( entity );
        }
    }

    @Override
    public int delRecord(TFilter filter) {
        String hql = "update VodRecord set status = -1 ";
        List<PropertyFilter> filterList = TransUtil.builderFilterList(filter);
        hql = TransUtil.buildQueryStringByPropertyFilter(hql, filterList);
        Query query = vodRecordDao.createQuery(hql);
        int executeNum = query.executeUpdate();
        boolean hasId = filter.getFilters().containsKey("EQL_id");
        if(hasId){
            vodAsyncService.interruptTask(Long.valueOf(filter.getFilters().get("EQL_id")));
        }else{
            vodAsyncService.interruptTaskByChannel(Integer.valueOf(filter.getFilters().get("EQI_channelId")));
        }
        return executeNum;
    }

//    @Override
//    @Transactional(propagation = Propagation.NOT_SUPPORTED, readOnly = true)
//    public boolean checkInRange(int fatherId, String startTime, String endTime, long excludeId) {
//        String hql = "select count(id) from VodRecord where channelId =? and status = 1 and  startTime =< ? and endTime >= ? and id != ?";
//        long resultNum = (Long) (vodRecordDao.createQuery(hql)
//                .setInteger(0, fatherId)
//                .setString(1, endTime)
//                .setString(2, startTime).setLong(3, excludeId)
//                .uniqueResult());
//        return resultNum > 0;
//    }

    @Override
    public VodRecord findRecordById(long id){
        return vodRecordDao.findUniqueBy("id",id);
    }
    @Override
    public List<VodRecord> findRecordList(TFilter filter){
        List<PropertyFilter> filterList = TransUtil.builderFilterList(filter);
        return vodRecordDao.find(filterList );
    }


}
